﻿using Ceremony.API.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ceremony.API.Infrastructure.EntityConfigurations
{
    public class UserEntityTypeConfiguration
        : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.ToTable("User");

            builder.HasKey(ci => ci.Id);

            builder.Property(ci => ci.Id)
               .UseHiLo("user_hilo")
               .IsRequired();

            builder.Property(cb => cb.UserName)
                .IsRequired(true)
                .HasMaxLength(100);
            builder.Property(cb => cb.RoleId)
               .IsRequired(false)
               .HasMaxLength(500);
            builder.Property(cb => cb.AuthorizeCode)
                .IsRequired(false)
                .HasMaxLength(100);
            builder.Property(cb => cb.OpenId)
               .IsRequired(true)
               .HasMaxLength(100);
            builder.Property(cb => cb.Nickname)
            .IsRequired(false)
            .HasMaxLength(500);
            builder.Property(cb => cb.Sex)
             .IsRequired(false)
             .HasMaxLength(50);
            builder.Property(cb => cb.CreateTime)
               .IsRequired(true);
            builder.Property(cb => cb.Province)
               .IsRequired(false)
               .HasMaxLength(100);
            builder.Property(cb => cb.City)
                .IsRequired(false)
                .HasMaxLength(100);
            builder.Property(cb => cb.Country)
                .IsRequired(false)
                .HasMaxLength(100);
            builder.Property(cb => cb.HeadImgUrl)
                .IsRequired(false)
                .HasMaxLength(500);
            builder.Property(cb => cb.UnionId)
                .IsRequired(false)
                .HasMaxLength(100);
            //frogin key
            builder.HasMany(b => b.UserPrizeRecords)
            .WithOne()
            .HasForeignKey("UserId")
            .OnDelete(DeleteBehavior.Cascade);

            var navigation = builder.Metadata.FindNavigation(nameof(User.UserPrizeRecords));
            navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
        }
    }
}
